package map;

import java.util.HashMap;

/**
 * 通过HashMap统计字符串中字符的个数
 */
public class Test04 {
    public static void main(String[] args) {
        HashMap<String, Integer> hashMap = new HashMap<>();
        String[] str = {"张三","李四","王五","1","d","李四","张三","李四","王五","1","d","李四","王五","1","d","李四","王五","1","d","李四",};
        count(hashMap,str);
        System.out.println(hashMap);
    }

    public static void count(HashMap<String, Integer> hashMap, String[] str) {
        for (String s : str) {
            System.out.println(s);
            if (hashMap.containsKey(s)) {
                Integer count = hashMap.get(s);
                count++;
                hashMap.put(s, count);
            }else {
                hashMap.put(s, 1);
            }
        }
    }
}
